﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using System.IO;
using AjaxControlToolkit;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;


namespace RedHouse.AdminControl.UserControls
{
    public partial class UCDishDetail : System.Web.UI.UserControl
    {
        public string Message { get; set; }
        private ILocationService _locationService;
        private ILanguageService _languageService;
        private ICategoryService _categoryService;
        private IDishService _dishService;

        private string _imagePath;
        protected void Page_Load(object sender, EventArgs e)
        {
            Initialize();
            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            RedHouseDish dish = GetDishFromView();
            OperationResult result = _dishService.SaveDish(dish);
            ErrorPanel.Visible = !result.Result;
            SuccessPanel.Visible = result.Result;
        }

        private RedHouseDish GetDishFromView()
        {
            RedHouseDish dish = new RedHouseDish()
            {
                Id = Guid.NewGuid(),
                LocationID = int.Parse(cboLocation.SelectedValue),
                CategoryID = new Guid(cboCategory.SelectedValue),
                LangId = new Guid(cboLanguage.SelectedValue),
                Title = txtName.Text,
                Description = txtDescription.Text,
                ImageUrl = txtImageUrl.Text,
                Rating = 0,
                CreatedBy = "admin",
                CreatedDate = DateTime.Now
            };
            return dish;
        }


        private void Initialize()
        {
            Message = string.Empty;
            ErrorPanel.Visible = false;
            SuccessPanel.Visible = false;

            _languageService = new LanguageService();
            _locationService = new LocationService();
            _categoryService = new CategoryService();
            _dishService = new DishService();

            //Databinding
            cboLocation.DataSource = _locationService.GetAllLocation();
            cboLocation.DataTextField = "Name";
            cboLocation.DataValueField = "ID";
            cboLocation.DataBind();

            cboLanguage.DataSource = _languageService.GetAllLanguage();
            cboLanguage.DataTextField = "Description";
            cboLanguage.DataValueField = "ID";
            cboLanguage.DataBind();

            cboCategory.DataSource = _categoryService.GetAll();
            cboCategory.DataTextField = "Title";
            cboCategory.DataValueField = "Id";
            cboCategory.DataBind();
        }
    }
}